package com.dianping.cat.alarm.rule.transform;

import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;








public class DefaultDomParser
  extends Object
  implements IParser<Node>
{
  protected Node getChildTagNode(Node parent, String name) {
    NodeList children = parent.getChildNodes();
/*  33 */     int len = children.getLength();

/*  35 */     for (int i = 0; i < len; i++) {
      Node child = children.item(i);

/*  38 */       if (child.getNodeType() == 1 && 
/*  39 */         child.getNodeName().equals(name)) {
/*  40 */         return child;
      }
    }


/*  45 */     return null;
  }

  protected List<Node> getChildTagNodes(Node parent, String name) {
/*  49 */     NodeList children = parent.getChildNodes();
/*  50 */     int len = children.getLength();
/*  51 */     List<Node> nodes = new ArrayList<Node>(len);

/*  53 */     for (int i = 0; i < len; i++) {
/*  54 */       Node child = children.item(i);

/*  56 */       if (child.getNodeType() == 1 && (
/*  57 */         name == null || child.getNodeName().equals(name))) {
/*  58 */         nodes.add(child);
      }
    }


/*  63 */     return nodes;
  }

  protected Node getDocument(String xml) {
/*  67 */     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

/*  69 */     dbf.setIgnoringElementContentWhitespace(true);
/*  70 */     dbf.setIgnoringComments(true);

    try {
/*  73 */       DocumentBuilder db = dbf.newDocumentBuilder();

/*  75 */       return db.parse(new InputSource(new StringReader(xml)));
/*  76 */     } catch (Exception x) {
/*  77 */       throw new RuntimeException(x);
    }
  }

  protected List<Node> getGrandChildTagNodes(Node parent, String name) {
/*  82 */     Node child = getChildTagNode(parent, name);
/*  83 */     NodeList children = (child == null) ? null : child.getChildNodes();
/*  84 */     int len = (children == null) ? 0 : children.getLength();
/*  85 */     List<Node> nodes = new ArrayList<Node>(len);

/*  87 */     for (int i = 0; i < len; i++) {
/*  88 */       Node grandChild = children.item(i);

/*  90 */       if (grandChild.getNodeType() == 1) {
/*  91 */         nodes.add(grandChild);
      }
    }

/*  95 */     return nodes;
  }


/*  99 */   public MonitorRules parse(Node node) { return parse(new DefaultDomMaker(), new DefaultLinker(false), node); }


  public MonitorRules parse(String xml) throws SAXException, IOException {
/* 103 */     Node doc = getDocument(xml);
/* 104 */     Node rootNode = getChildTagNode(doc, "monitor-rules");

/* 106 */     if (rootNode == null) {
/* 107 */       throw new RuntimeException(String.format("monitor-rules element(%s) is expected!", new Object[] { "monitor-rules" }));
    }

/* 110 */     return parse(new DefaultDomMaker(), new DefaultLinker(false), rootNode);
  }

  public MonitorRules parse(IMaker<Node> maker, ILinker linker, Node node) {
/* 114 */     MonitorRules monitorRules = maker.buildMonitorRules(node);

/* 116 */     if (node != null) {
/* 117 */       MonitorRules parent = monitorRules;

/* 119 */       for (Node child : getChildTagNodes(node, "rule")) {
/* 120 */         Rule rule = maker.buildRule(child);

/* 122 */         if (linker.onRule(parent, rule)) {
/* 123 */           parseForRule(maker, linker, rule, child);
        }
      }
    }

/* 128 */     return monitorRules;
  }

  public void parseForCondition(IMaker<Node> maker, ILinker linker, Condition parent, Node node) {
/* 132 */     for (Node child : getChildTagNodes(node, "sub-condition")) {
/* 133 */       SubCondition subCondition = maker.buildSubCondition(child);

/* 135 */       if (linker.onSubCondition(parent, subCondition)) {
/* 136 */         parseForSubCondition(maker, linker, subCondition, child);
      }
    }
  }

  public void parseForConfig(IMaker<Node> maker, ILinker linker, Config parent, Node node) {
/* 142 */     for (Node child : getChildTagNodes(node, "condition")) {
/* 143 */       Condition condition = maker.buildCondition(child);

/* 145 */       if (linker.onCondition(parent, condition)) {
/* 146 */         parseForCondition(maker, linker, condition, child);
      }
    }
  }


  public void parseForMetricItem(IMaker<Node> maker, ILinker linker, MetricItem parent, Node node) {}

  public void parseForRule(IMaker<Node> maker, ILinker linker, Rule parent, Node node) {
/* 155 */     for (Node child : getChildTagNodes(node, "metric-item")) {
/* 156 */       MetricItem metricItem = maker.buildMetricItem(child);

/* 158 */       if (linker.onMetricItem(parent, metricItem)) {
/* 159 */         parseForMetricItem(maker, linker, metricItem, child);
      }
    }

/* 163 */     for (Node child : getChildTagNodes(node, "config")) {
/* 164 */       Config config = maker.buildConfig(child);

/* 166 */       if (linker.onConfig(parent, config))
/* 167 */         parseForConfig(maker, linker, config, child); 
    }
  }

  public void parseForSubCondition(IMaker<Node> maker, ILinker linker, SubCondition parent, Node node) {}
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/rule/transform/DefaultDomParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */